package com.superbytecode.stencil.utils;

import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.MapUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: magician
 * @Package: com.superbytecode.magician.utils
 * @ClassName: MapUtil
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/8 19:29
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@UtilityClass
public class MapUtil {

    public String obtainIgnoreCaseString(Map<String, Object> map, String key) {
        return getString(map, key);
    }

    public String obtainIgnoreCaseString(Map<String, Object> map, String key, String defaultValue) {
        return Optional.ofNullable(getString(copyMap(map), key.toUpperCase())).orElse(defaultValue);
    }

    public String getString(Map<String, Object> map, String key) {
        return getString(map, key, null);
    }

    public String getString(Map<String, Object> map, String key, String defaultValue) {
        return MapUtils.getString(map, key, null);
    }

    private Map<String, Object> copyMap(Map<String, Object> orgMap) {
        Map<String, Object> resultMap = new HashMap<>();
        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }
        Set<Map.Entry<String, Object>> entrySet = orgMap.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            Object value = entry.getValue();
            resultMap.put(key.toUpperCase(), value);
        }
        return resultMap;
    }
}